import { ref, reactive, toRefs } from "vue";
import type { UnwrapRef } from "vue";
import type { FormInstance } from "ant-design-vue";
import type { Rule } from "ant-design-vue/es/form";
import type { formInterface } from "./interface";
// validate
import { validateRequired } from "@/utils/index";
export function useFormMoudel(formRef?: any) {
  const state = reactive<any>({
    a00030: "",
    a00031: "",
    a00032: "",
    a00033: "",
    a00034: "",
    a00035: "",
  });
  const fields: UnwrapRef<formInterface> = reactive({
    a0001: "",
    a0002: 1,
    a0003: "",
    a0004: "",
    a0005: "",
    a0006: "",
    a0007: "",
    a0008: "",
    a0009: undefined,
    a00010: "",
    a00011: "",
    a00012: "",
    a00013: "",
    a00014: undefined,
    a00015: "",
    a00016: "",
    a00017: "",
    a00018: 1,
    a00019: 1,
    a00020: 1,
    a00021: 1,
    a00022: "",
    a00023: "",
    a00024: "",
    a00025: "",
    a00026: "",
    a00027: "",
    a00028: "",
    a00029: "",
    a00030: "",
    a00031: "",
    a00032: "",
    a00033: "",
    a00034: "",
    a00035: "",
    a00036: "",
    a00037: "",
    a00038: "",
    a00039: "",
    a00040: "",
    a00041: "",
    a00042: "",
    a00043: "",
    a00044: "",
    a00045: "",
    a00046: "",
    a00047: "",
    a00048: "",
    a00049: "",
  });

  const rules: Record<string, Rule[]> = {
    a0001: [validateRequired() as object],
    a0004: [validateRequired() as object],
    a0005: [validateRequired() as object],
    a00010: [validateRequired("change") as object],
    a00011: [validateRequired() as object],
    a00014: [validateRequired("change") as object],
  };

  /**
   * 测试数据还原
   * @returns
   */
  const userInnerLoading = () => {
    return new Promise<void>((resolve, reject) => {
      setTimeout(() => {
        fields.a0001 = "213";
        fields.a0002 = 1;
        fields.a0003 = "123";
        fields.a0004 = "123";
        fields.a0005 = "123";
        fields.a0006 = "123";
        fields.a0007 = "123";
        fields.a0008 = "123";
        fields.a0009 = 3;
        fields.a00010 = "123";
        fields.a00011 = "123";
        fields.a00012 = "123";
        fields.a00013 = "123";
        fields.a00014 = 2;
        fields.a00015 = "123";
        fields.a00016 = "123";
        fields.a00017 = "13";
        fields.a00018 = 1;
        fields.a00019 = 1;
        fields.a00020 = 1;
        fields.a00021 = 1;
        fields.a00022 = "12313";
        fields.a00023 = "123";
        fields.a00024 = "123";
        fields.a00025 = "123";
        fields.a00026 = "23";
        fields.a00027 = "123";
        fields.a00028 = "123";
        fields.a00029 = "132";
        fields.a00030 = "123";
        fields.a00031 = "13";
        fields.a00032 = "123";
        fields.a00033 = "123";
        fields.a00034 = "132";
        fields.a00035 = "13";
        fields.a00036 = "123";
        fields.a00037 = "13";
        fields.a00038 = "123";
        fields.a00039 = "123";
        fields.a00040 = "123";
        fields.a00041 = "123";
        fields.a00042 = "123";
        fields.a00043 = "123";
        fields.a00044 = "13";
        fields.a00045 = "123";
        fields.a00046 = "123";
        fields.a00047 = "123";
        fields.a00048 = "123123";
        fields.a00049 = "123";

        state.a00030 = "简介（中文）";
        state.a00031 = "简介（英文";
        state.a00032 = "研究方向（中文）";
        state.a00033 = "研究方向（英文）";
        state.a00034 = "往期学员论文（中文）";
        state.a00035 = "往期学员论文（英文）";
        resolve();
      }, 500);
    });
  };
  /**
   * 点击确定按钮，校验表单
   * @returns
   */
  const useBeforeChange = () => {
    console.log("fields", fields);
    return new Promise<void>((resolve, reject) => {
      formRef.value
        .validate()
        .then((res: any) => {
          console.log(11);
        })
        .catch(() => {
          reject();
        });
    });
  };

  return {
    fields,
    rules,
    state,
    userInnerLoading,
    useBeforeChange,
  };
}
